<template>
  <el-container>
    <!-- Header area -->
    <el-header>
      <div>
        <img src="../assets/flyan.jpg" alt />
        <span>Chenu e-commerce background management system</span>
      </div>
      <el-button type="info" @click="logout">Logout</el-button>
    </el-header>
    <!-- Main Context -->
    <el-container>
      <!-- Sidebar area -->
      <el-aside :width="isCollapsed ? '64px' : '200px'">
        <!-- controll of menu off/on display -->
        <div class="toggle-btn" @click="toggleCollapse">{{toggleButtonText}}</div>
        <!-- menu -->
        <el-menu
          background-color="#333744"
          text-color="#ffffff"
          active-text-color="#409eff"
          unique-opened
          :collapse="isCollapsed"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- 1 level menu -->
          <el-submenu :index="oe.id.toString()" :key="oe.id" v-for="oe in menuList">
            <template slot="title">
              <i :class="icons[oe.authName]"></i>
              <span>{{oe.authName}}</span>
            </template>
            <!-- 2 level menu -->
            <el-menu-item
              :index="'/' + te.path"
              :key="te.id"
              v-for="te in oe.children"
              @click="saveNavActiveState('/' + te.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{te.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- Right content area -->
      <el-main>
        <!-- Route placeholder -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data: () => {
    return {
      /* left menu list data */
      menuList: [],
      /* per icon of menus */
      icons: {
        用户管理: 'iconfont icon-users',
        权限管理: 'iconfont icon-tijikongjian',
        商品管理: 'iconfont icon-shangpin',
        订单管理: 'iconfont icon-danju',
        数据统计: 'iconfont icon-baobiao'
      },
      isCollapsed: false,
      toggleButtonText: '<<<',
      activePath: ''
    }
  },
  methods: {
    /* logout, first clear token, go to '/login' */
    logout: function() {
      window.sessionStorage.clear()
      this.$msg.success('Logout success!')
      this.$router.push('/login')
    },
    /* get all menu list data */
    getMenuList: async function() {
      const rs = await this.$http.get('menus')
      if (rs.meta.status !== 200) return this.$msg.error(rs.meta.msg)
      // console.log(JSON.stringify(rs.data));
      // this.$msg.success(rs.meta.msg)
      this.menuList = rs.data
    },
    /* controll toggle collapse state of menu */
    toggleCollapse: function() {
      this.isCollapsed = !this.isCollapsed
      if (!this.isCollapsed) {
        this.toggleButtonText = '<<<'
      } else {
        this.toggleButtonText = '>>>'
      }
    },
    saveNavActiveState: function(path) {
      window.sessionStorage.setItem('activePath', path)
      this.activePath = path
    }
  },
  /* when vue be created, auto callback this func */
  created: function() {
    /* get menu list data from server */
    this.getMenuList()
    /* get active path from session storage */
    this.activePath = window.sessionStorage.getItem('activePath')
  }
}
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #ffffff;
  font-size: 21px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 11px;
}
.toggle-btn {
  background-color: #4a5064;
  color: #ffffff;
  font-size: 13px;
  line-height: 25px;
  text-align: center;
  letter-spacing: 0.1em;
  cursor: pointer;
}
</style>
